package com.hzqy.service.behaviorControl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.hzqy.persistence.dao.behaviorControl.BehaviorControlDao;
import com.hzqy.service.core.GenericServiceBean;
import com.hzqy.web.vo.behaviorControl.IndustrySchedulesVo;

@Service
public class BehaviorControlServiceBean extends GenericServiceBean<IndustrySchedulesVo, BehaviorControlDao, Integer> {
	private static final long serialVersionUID = -8693530965475436557L;
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	
	private static final DateFormat MINUTES = new SimpleDateFormat("HH:mm:ss");
	
	@Resource
	private BehaviorControlDao behaviorControlDao;
	
	@Override
	public BehaviorControlDao getEntityDao() {
		return behaviorControlDao;
	}

	public Map<String, Object> forcedSwitchingByServerid(int orgId, String serverid, int timeSpan) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("orgId", orgId);
		paramMap.put("serverid", serverid);
		paramMap.put("timeSpan", timeSpan);
		
		Calendar startDate = Calendar.getInstance();
		startDate.setTime(new Date());
		paramMap.put("startDate", format(startDate.getTime()));
		
		Calendar endDate = Calendar.getInstance();
		endDate.add(Calendar.SECOND, timeSpan);
		paramMap.put("endDate", format(endDate.getTime()));
		
		int week_now = startDate.get(Calendar.DAY_OF_WEEK);//1周日 2周一 .... 7周六
		week_now = week_now == 1 ? 7 : week_now - 1;
		paramMap.put("weekNow", week_now);
		
		IndustrySchedulesVo nextSchedulesVo = behaviorControlDao.forcedSwitchingByServerid(paramMap);
		
		resultMap.put("hasNextSchedulesVo", nextSchedulesVo != null);
		if(nextSchedulesVo != null) {
			//轮播强切需要特殊处理
			if(nextSchedulesVo.getF_is_type() == 4) {
				String contentIds = behaviorControlDao.selectContentIdsByIndustrySchedule(nextSchedulesVo);
				nextSchedulesVo.setContentIds(contentIds);
			}
			resultMap.put("schedulesVo", nextSchedulesVo);
		}
		
		return resultMap;
	}
	public static final String format(Date date) {
		long time = date.getTime();
		DateTime dateTime = new DateTime(time);
		return dateTime.toString("HH:mm:ss");
	}
}
